<template>
  <el-dialog
    :title="!dataForm.milestoneId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="榜单成员id" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="榜单成员id"></el-input>
    </el-form-item>
    <el-form-item label="榜单成员名称" prop="memberName">
      <el-input v-model="dataForm.memberName" placeholder="榜单成员名称"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="发生时间" prop="happenTime">
      <el-input v-model="dataForm.happenTime" placeholder="发生时间"></el-input>
    </el-form-item>
    <el-form-item label="事件内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="事件内容"></el-input>
    </el-form-item>
    <el-form-item label="点赞数量" prop="likeCount">
      <el-input v-model="dataForm.likeCount" placeholder="点赞数量"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人id"></el-input>
    </el-form-item>
    <el-form-item label="创建人名称" prop="createUserName">
      <el-input v-model="dataForm.createUserName" placeholder="创建人名称"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          milestoneId: 0,
          memberId: '',
          memberName: '',
          title: '',
          happenTime: '',
          content: '',
          likeCount: '',
          isDelete: '',
          createUserId: '',
          createUserName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '榜单成员id不能为空', trigger: 'blur' }
          ],
          memberName: [
            { required: true, message: '榜单成员名称不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          happenTime: [
            { required: true, message: '发生时间不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '事件内容不能为空', trigger: 'blur' }
          ],
          likeCount: [
            { required: true, message: '点赞数量不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createUserName: [
            { required: true, message: '创建人名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.milestoneId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.milestoneId) {
            this.$http({
              url: this.$http.adornUrl(`/rank/milestone/info/${this.dataForm.milestoneId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.milestone.memberId
                this.dataForm.memberName = data.milestone.memberName
                this.dataForm.title = data.milestone.title
                this.dataForm.happenTime = data.milestone.happenTime
                this.dataForm.content = data.milestone.content
                this.dataForm.likeCount = data.milestone.likeCount
                this.dataForm.isDelete = data.milestone.isDelete
                this.dataForm.createUserId = data.milestone.createUserId
                this.dataForm.createUserName = data.milestone.createUserName
                this.dataForm.createTime = data.milestone.createTime
                this.dataForm.updateTime = data.milestone.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/milestone/${!this.dataForm.milestoneId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'milestoneId': this.dataForm.milestoneId || undefined,
                'memberId': this.dataForm.memberId,
                'memberName': this.dataForm.memberName,
                'title': this.dataForm.title,
                'happenTime': this.dataForm.happenTime,
                'content': this.dataForm.content,
                'likeCount': this.dataForm.likeCount,
                'isDelete': this.dataForm.isDelete,
                'createUserId': this.dataForm.createUserId,
                'createUserName': this.dataForm.createUserName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
